<template>
  <div class="home">
    <el-button @click="linkToCreate">创建</el-button>
    <hr>
    <el-table
      v-loading="isLoading"
      element-loading-text="加载中..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255, 0, 0, 0.3)"
      border
      row-class-name="my-row"
      :row-style="{backgroundColor: '#fff'}"
      empty-text="no data!!!"
      :data="tableData"
      @row-click="onRowClick"
      style="width: 100%">
      <el-table-column
        label="ID"
        width="280"
        prop="id"
      >
      </el-table-column>
      <el-table-column
        label="标题"
        prop="title"
      >
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click.stop="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click.stop="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from 'axios'
import { mapState, mapActions } from 'vuex'

export default {
  name: 'AdminNews',
  data() {
    return {
      isLoading: true,
      // tableData: []
    }
  },
  computed: {
    ...mapState('admin_news', ['tableData'])
  },
  created() {
    this.getNewsList()
  },
  methods: {
    ...mapActions('admin_news', ['getList']),
    onRowClick(row) {
      console.log(row.id)
      this.$router.push({
        name: 'admin-news-show',
        params: {
          newsId: row.id
        }
      })
    },
    async getNewsList() {
      try {
        this.isLoading = true
        await this.getList()
      } catch (e) {
        this.$notify.error({
          title: '错误',
          message: '网络错误请刷新重试！'
        });
      } finally {
        this.isLoading = false
      }
    },
    handleEdit(row) {
      this.$router.push({
        name: 'admin-news-edit',
        params: {
          newsId: row.id
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await axios({
            method: 'DELETE',
            url: `http://localhost:3000/news/${row.id}`
          })
          this.getNewsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        } catch (e) {
          this.$message({
            type: 'error',
            message: '删除失败！'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    linkToCreate() {
      this.$router.push({
        name: 'admin-news-create'
      })
    }
  }
}
</script>

<style>

  .el-table td, .el-table th.is-leaf {
    border-color: red;
  }

  .el-table--enable-row-hover .el-table__body tr:hover>td {
    background-color: red;
  }

</style>
